
var app = getApp();
var config = require('../../../../config.js');
const http = require('../../../../http.js')
Page({

  /**
   * 页面的初始数据
   */
  /**
   * 页面的初始数据
   */
  data: {
    downloadImgSrc:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getcalendar();//获取日历
  },

  onClick: function (e) {
    let type = parseInt(e.currentTarget.dataset.type)
    switch (type) {
      //返回
      case 1:
        wx.navigateBack({
          delta: 1  // 返回上一级页面
        })
        break;
        //下载
      case 2:
        this.downloadimg();
        break;
    }
  },
  /**
   * 每次打开页面都会调用一次。
   */
  onShow: function () {
  },
  onUnload: function () {
  },
  
  getcalendar:function(){
    var that = this;
    http.request(
      config.calendar,
      'GET',null,
      function (res) {
        if(res.data != null && res.data !=""){
          that.setData({
            downloadImgSrc: res.data.imgUrl,
          })
        }else{
          wx.showToast({
            title: "获取数据失败！",
            icon: 'none',
          })
        }
       
      },
      function (err) {
        wx.showToast({
          title: err,
          icon: 'none',
        })
      }, true
    )

  },

  downloadimg: function() {
    var that = this
    // 通过 wx.getSetting 先查询一下用户是否授权了 "scope.writePhotosAlbum" 这个 scope
    wx.getSetting({
      success(res) {
        if (!res['scope.writePhotosAlbum']) {
          wx.authorize({ 
            scope: 'scope.writePhotosAlbum', 
            success() {
              wx.downloadFile({ 
                url:that.data.downloadImgSrc,
                success: function(res) {
                  wx.saveImageToPhotosAlbum({
                    filePath:res.tempFilePath,
                    success(res) {
                      console.log(res);
                      wx.showToast({
                        title: "保存成功",
                        duration: 2000 
                      }) 
                      that.hideModal()
                    }  
                  }) 
                }  
              })
            }
          })
        }else {
          console.log('error');
        }  
      }  
    }) 
  },

  next:function(){


  }


})